package leetcode;

import java.util.HashMap;
import java.util.Map;

public class Leetcode560 {
    public int subarraySum(int[] nums, int k) {
        int n = nums.length;
        Map<Integer,Integer> m = new HashMap<>();
        int count = 0;
        m.put(0,1);//添加一个前缀为0的出现1次
        int sum = 0;
        for(int i = 0;i<nums.length;i++){
            sum = sum+nums[i];
            if(m.containsKey(sum-k)){
                count = count+m.get(sum-k);
            }
            m.put(sum,m.getOrDefault(sum,0)+1);
        }
        return count;
    }
}
